package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.business.domain.FileExternalUnitsFormPerson;
import com.ruoyi.business.domain.bo.FileExternalUnitsFormPersonBo;
import com.ruoyi.business.domain.vo.FileExternalUnitsFormPersonVo;
import com.ruoyi.business.mapper.FileExternalUnitsFormPersonMapper;
import com.ruoyi.business.service.IFileExternalUnitsFormPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class FileExternalUnitsFormPersonServiceImpl implements IFileExternalUnitsFormPersonService {
    @Autowired
    private FileExternalUnitsFormPersonMapper baseMapper;
    @Override
    public Boolean insertByBo(FileExternalUnitsFormPersonBo bo) {
        FileExternalUnitsFormPerson add = BeanUtil.toBean(bo, FileExternalUnitsFormPerson.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    private MPJLambdaWrapper<FileExternalUnitsFormPerson> buildQueryWrapper(FileExternalUnitsFormPersonBo bo) {
        MPJLambdaWrapper<FileExternalUnitsFormPerson> joinQueryWrapper = new MPJLambdaWrapper<>();
        joinQueryWrapper.selectAll(FileExternalUnitsFormPerson.class)
            .eq(bo.getFormId()!= null, FileExternalUnitsFormPerson::getFormId, bo.getFormId())
            .eq(bo.getPersonId()!= null, FileExternalUnitsFormPerson::getPersonId, bo.getPersonId())
            .groupBy(FileExternalUnitsFormPerson::getId);
        return joinQueryWrapper;
    }

    @Override
    public List<Long> queryByFromId(FileExternalUnitsFormPersonBo bo) {
        MPJLambdaWrapper<FileExternalUnitsFormPerson> joinQueryWrapper = buildQueryWrapper(bo);
        List<FileExternalUnitsFormPersonVo> voList = baseMapper.selectJoinList(FileExternalUnitsFormPersonVo.class, joinQueryWrapper);
        ArrayList<Long> personIDs = new ArrayList<>();
        for (FileExternalUnitsFormPersonVo fileExternalUnitsFormPersonVo : voList) {
            personIDs.add(fileExternalUnitsFormPersonVo.getPersonId());
        }
        return personIDs;
    }

    @Override
    public Boolean deleteByPersonId(Long personId) {
        baseMapper.delete(new LambdaQueryWrapper<FileExternalUnitsFormPerson>().eq(FileExternalUnitsFormPerson::getPersonId, personId));
        return null;
    }

    @Override
    public Boolean deleteByFromId(Long fromId) {
        baseMapper.delete(new LambdaQueryWrapper<FileExternalUnitsFormPerson>().eq(FileExternalUnitsFormPerson::getFormId, fromId));
        return null;
    }
}
